﻿using System;
using Com.Pernix.Component.Member;
using Com.Pernix.Component.Member.BL.Repositories;
using Com.Pernix.Component.Test.Mapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace Com.Pernix.Common.Log.TestConsole
{
    class Program
    {
        const string ConnectionString = @"Server=PAWAT-PC;Database=OnlineExam;User Id=Sa;Password=Sa111111;";

        static void TestRepos()
        {
            var unitOfWork = new UnitOfWork(NHibernateHelper.SessionFactory);
            using (var userRepos = new UserRepository(unitOfWork))
            {
                /*
                var user7 = new User
                                {
                                    Username = "Pawat7",
                                    FirstName = "Pawat",
                                    LastName = "Iamsupa",
                                    CreateDate = DateTime.Now,
                                    Password = "Aa123456",
                                    UpdateDate = DateTime.Now
                                };
             
                userRepos.Add(user7);
                userRepos.Commit();
                 */
                var xxx = userRepos.GetByUsernameStartWith("Pawat");

            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Start Console...");
            Console.WriteLine("Create DB");
            TestRepos();
            //var factory = CreateSessionFactory();
            //using (var session = factory.OpenSession())
            //{

            //    // do something with the session
            //    //var category = new Category
            //    //                   {
            //    //                       Name = "Beveraged2",
            //    //                       Description = "Some description"
            //    //                   };
            //    //var id = session.Save(category);
            //    session.BeginTransaction();
            //    var categoryObj = session.Get<Category>(1);
            //    session.Delete(categoryObj);
            //    session.Transaction.Commit();



            //    //var product = new Product
            //    //                  {
            //    //                      Name="Test Product",
            //    //                      Category = categoryObj
            //    //                  };
            //    //session.Save(product);

            //}

            Console.WriteLine("Finished.");
            Console.ReadKey();
        }
        private static void CreateDatabase()
        {
            Fluently.Configure().Database(MsSqlConfiguration.MsSql2008.ConnectionString(ConnectionString)).Mappings(
                m => m.FluentMappings.AddFromAssemblyOf<ProductMap>())
                .ExposeConfiguration(CreateSchema).BuildConfiguration();
        }
        private static void CreateSchema(Configuration cfg)
        {
            var schemaExport = new SchemaExport(cfg);
            schemaExport.Drop(false, true);
            schemaExport.Create(false, true);
        }

        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure().Database(MsSqlConfiguration.MsSql2008.ConnectionString(ConnectionString)).Mappings(
                    m => m.FluentMappings.AddFromAssemblyOf<ProductMap>()).BuildSessionFactory();
        }
    }
}
